<?php

namespace plugin\cms\app\controller\admin;

use app\bll\ComponentExtend as BllComponentExtend;
use app\bll\ComponentExtendGroup as BllComponentExtendGroup;
use core\controller\AdminBase;
use Exception;
use plugin\cms\app\bll\Category as BllCategory;
use support\View;
use plugin\cms\app\bll\AipublishKeyword as BllAipublishKeyword;
use support\Db;

class AiPublish extends AdminBase
{
    public function index()
    {
        $bllAipublishKeyword = new BllAipublishKeyword();
        $keywords = $bllAipublishKeyword->paginate(
            siteid: request()->nowsiteid
        );
        View::assign('keywords', $keywords);
        return $this->view();
    }

    /**
     * 添加关键词
     *
     * @return void
     */
    public function addKeyword()
    {
        if (is_post()) {
            try {
                $keywords = request()->post('keywords');
                $keywords = str_replace("\r\n", "\n", $keywords);
                $keywords = explode("\n", $keywords);
                $data     = request()->post();
                unset($data['keywords']);
                $bllAipublishKeyword = new BllAipublishKeyword();

                foreach ($keywords as $keyword) {
                    if (trim($keyword) == '') {
                        continue;
                    }
                    $data['keyword'] = trim($keyword);
                    $data['siteid'] = request()->nowsiteid;
                    $bllAipublishKeyword->add($data);
                }
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $bllComponentExtend = new BllComponentExtend();
            $extends            = $bllComponentExtend->cascader(groupid: 3);

            $bllCategory = new BllCategory();
            $cates     = $bllCategory->cascader(request()->nowsiteid);

            View::assign('extends', $extends);
            View::assign('cates', $cates);
            return $this->view();
        }
    }

    /**
     * 关键词编辑
     *
     * @return mixed
     */
    public function editKeyword()
    {
        if (is_post()) {
            try {
                $bllAipublishKeyword = new BllAipublishKeyword();
                $data = request()->post();
                $data['siteid'] = request()->nowsiteid;
                $bllAipublishKeyword->edit(
                    $data,
                    request()->get('id'),
                    request()->nowsiteid
                );
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $bllComponentExtend = new BllComponentExtend();
            $extends            = $bllComponentExtend->cascader(groupid: 3);

            $bllCategory = new BllCategory();
            $cates     = $bllCategory->cascader(request()->nowsiteid);

            $id = request()->get('id');
            $bllAipublishKeyword = new BllAipublishKeyword();
            $keyword = $bllAipublishKeyword->find($id, request()->nowsiteid);

            View::assign('extends', $extends);
            View::assign('cates', $cates);
            View::assign('keyword', $keyword);
            return $this->view();
        }
    }


    /**
     * 删除关键词
     *
     * @return mixed
     */
    public function delKeyword()
    {
        try {
            $id = request()->get('keywordid');
            $bllAipublishKeyword = new BllAipublishKeyword();
            $bllAipublishKeyword->delete($id, request()->nowsiteid);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
